/**
 * 
 */
package org.mspring.mlog.web.module.common;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Attachment;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.service.AttachmentService;
import org.mspring.mlog.support.log.Log;
import org.mspring.mlog.web.module.admin.Admin_AbstractWidget;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.ResponseEntity;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 用户附件上传
 * 
 * @author Gao Youbo
 * @since 2012-11-22
 * @Description
 * @TODO
 */
@Widget
@RequestMapping("/common/attachment")
public class Common_AttachmentWidget extends Admin_AbstractWidget {
    @Autowired
    private AttachmentService attachmentService;

    @ResponseBody
    @RequestMapping("/upload")
    @Log
    public ResponseEntity upload(@RequestParam(required = false) String fkey, @RequestParam(required = false) String ftype, HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        User user = SecurityUtils.getCurrentUser(request);
        if (user == null) {
            rsp.setSuccess(false);
            rsp.setMessage("登录后才能上传附件");
            return rsp;
        }
        if (StringUtils.isBlank(fkey) || StringUtils.isBlank(ftype)) {
            rsp.setSuccess(false);
            rsp.setMessage("请检查fkey或ftype是否设置正确");
            return rsp;
        }
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            if (fileMap == null || fileMap.size() < 1) {
                return null;
            }
            MultipartFile mf = null;
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                mf = entity.getValue();
                break;
            }
            Attachment attachment = attachmentService.createAttachment(mf, ftype, fkey);
            rsp.setSuccess(true);
            rsp.put("attach", attachment);
            return rsp;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            rsp.setSuccess(false);
            rsp.setMessage(e.getMessage());
            return rsp;
        }
    }

    @RequestMapping("/uploadview")
    public String uploadview(@RequestParam(required = false) String fkey, @RequestParam(required = false) String ftype, HttpServletRequest request, HttpServletResponse response, Model model) {
        User user = SecurityUtils.getCurrentUser(request);
        if (user == null) {
            return prompt(model, "请先登录");
        }
        if (StringUtils.isBlank(fkey) || StringUtils.isBlank(ftype)) {
            return prompt(model, "请检查fkey或ftype是否设置正确");
        }
        model.addAttribute("fkey", fkey);
        model.addAttribute("ftype", ftype);
        return "/common/attachment/uploadview";
    }

    @RequestMapping("/attachments")
    public String attachments(@RequestParam(required = false) String fkey, @RequestParam(required = false) String ftype, HttpServletRequest request, HttpServletResponse response, Model model) {
        User user = SecurityUtils.getCurrentUser(request);
        if (user == null) {
            return prompt(model, "请先登录");
        }
        if (StringUtils.isBlank(fkey) || StringUtils.isBlank(ftype)) {
            return prompt(model, "请检查fkey或ftype是否设置正确");
        }
        List<Attachment> attachments = attachmentService.findAttachments(fkey, ftype);
        model.addAttribute("attachments", attachments);
        model.addAttribute("fkey", fkey);
        model.addAttribute("ftype", ftype);
        return "/common/attachment/attachments";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        User user = SecurityUtils.getCurrentUser(request);
        if (user == null) {
            return prompt(model, "请先登录");
        }
        try {
            attachmentService.deleteAttachment(id, user.getId());
        } catch (Exception e) {
            // TODO: handle exception
            return "false";
        }
        return "true";
    }
}
